<script type="text/javascript">
    $(document).ready(function(){
        $('#year').click(function(){
            $('#startdate').val('');
            $('#end').val('');
            
        });
        $('#startdate').click(function(){
            $(this).val('');
            //$('#year').attr('disabled','disabled');
        })
        $('#end').click(function(){
            $(this).val('');
            //$('#year').attr('disabled','disabled');
        })
    });
    function getPDF() {
        var data = $('#reportForm').serialize();
        var url = 'index.php?r=pdf/jobreport&' + data;
        window.open(url);
    }
    function getSinglePDF() {
        var data = $('#jobForm').serialize();
        var url = 'index.php?r=pdf/job&' + data;
        window.open(url);
    }
    function showReport() {
        var panel;
        var data = $('#reportForm').serialize();
        console.log(data);
        var jobStore = Ext.create('Ext.data.Store', {
            model:'Jobs',
            autoLoad:true,
            proxy:{
                type:'ajax',
                api:{
                    read:'index.php?r=report/report/list&' + data

                },

                reader:{
                    type:'json',
                    totalProperty:'totalCount',
                    root:'data'
                }
            }
        });
     
        console.log(jobStore);
        var grid = Ext.create('Ext.grid.Panel', {
            width:grid_width,
            height:350,
            store:jobStore,
            title:'Jobs List',

            viewConfig:{
                stripeRows:true
            },
            dockedItems:[
                {
                    xtype:'pagingtoolbar',
                    store:jobStore,
                    dock:'bottom',
                    displayInfo:true
                }
            ],
            columns:[
                {
                    text:'Job ID',
                    flex:1,
                    dataIndex:'jobid',
                    renderer:function (value, p, record) {
                        return Ext.String.format("<a href='index.php?r=jobs/update&jobid={0}'>{0}</a>", record.data.jobid);
                    }
                },
                {
                    text:'Customer Number',
                    flex:1,
                    dataIndex:'custno'
                },
                {
                    text:'Location Name',
                    flex:1,
                    dataIndex:'locname'
                },
                {
                    text:'Description',
                    flex:1,
                    dataIndex:'description'
                },
                {
                    text:'Job Status',
                    flex:1,
                    dataIndex:'jobstatustext'
                },
                {
                    text:'Billing Status',
                    flex:1,
                    dataIndex:'billingstatustext'
                } ,
                {
                    text:'Start Date',
                    flex:1,
                    dataIndex:'start'
                }
            ]
        });
        if (panel) {
            panel.destroy();
        }

        panel = Ext.create('Ext.window.Window', {
            title:'Jobs Report',
            width:600,
            height:400,
            layout:'fit',
            closeAction:'destroy',
            items:grid,
            buttons:[

                {
                    text:'Close',
                    handler:function () {
                        panel.destroy();
                    }
                }
            ]
        });

        panel.show();
    }

</script>
<?php
$this->breadcrumbs = array(
    'Report',
);
?>
<div class="wrap">
    <div class="inside">
        <h1>Jobs report</h1>

        <div class="form">

            <?php
            echo CHtml::form('', 'post', array('id' => 'reportForm'));
            ?>
            <div class="row">
                <?php echo CHtml::label('Customer', 'custno'); ?>
                <?php
                $data = array('All' => 'All');
                $data += CHtml::listData(Customers::model()->findAll(), 'custno', 'lastname');
                echo CHtml::dropDownList('custno', '', $data);
                ?>
            </div>

            <div class="row">
                <?php echo CHtml::label('Start date', 'startdate'); ?>
                <?php
                $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                    'name' => 'startdate',
                    //  'value' => CTimestamp::formatDate('m/d/Y', time()),
                    'options' => array('startdate' => 'm-d-yy'),
                ));
                ?>
            </div>
            <div class="row">
                <?php echo CHtml::label('End date', 'end'); ?>
                <?php
                $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                    'name' => 'end',
                    //  'value' => CTimestamp::formatDate('m/d/Y', time()),
                    'options' => array('start' => 'm-d-yy'),
                ));
                ?>
            </div>
            <div class="row">
                <label>Year</label>
                <?php 
                $yearList = Yii::app()->db->createCommand('select distinct year(addeddate) addedYear from tbl_jobs order by addedYear')->queryAll();
                $list =array();
                foreach($yearList as $row){
                    $list[$row['addedYear']]=$row['addedYear'];
                }
                echo CHtml::dropDownList('year', '', $list);
                ?>
            </div>
            <div class="row">
                <?php echo CHtml::label('Job status', 'status'); ?>
                <?php
                $data = array('All' => 'All');
                $data += CHtml::listData(JobsStatus::model()->findAll('inactive=0'), 'id', 'description');
                echo CHtml::dropDownList('status', '', $data);
                ?>
            </div>
            <div class="row">
                <label>Sales Person</label>
                <?php echo CHtml::dropDownList('jobsalesperson', '', Employees::model()->getSalePersonList()); ?>
            </div>
            <div class="row">
<?php echo CHtml::link('View Report in pop up windows', '#', array('id' => 'report',
    'onclick' => 'javascript:showReport();')) ?>
            </div>
            <div class="row">
            <?php echo CHtml::link('Download in PDF', '#', array('id' => 'reportPDF',
                'onclick' => 'javascript:getPDF();')) ?>
            </div>
            <?
            echo CHtml::endForm();
            ?>
            <div id="job-list"></div>

            <h1>Singe Job report</h1>
            <?php
            echo CHtml::form('', 'post', array('id' => 'jobForm'));
            ?>
            <div class="row">
                <?php echo CHtml::label('Job no', 'jobid'); ?>
                <?php echo CHtml::textField('jobid') ?>
            </div>
            <?
            echo CHtml::endForm();
            ?>
        </div>
        <div>
        <?php echo CHtml::link('Download in PDF', '', array(
            'onclick' => 'javascript:getSinglePDF();')) ?>
        </div>

    </div>
</div>
